package logic

import (
	"context"
	"fmt"
	"product-mall/models/mysql"

	"product-mall/productrpc/internal/svc"
	"product-mall/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProductLogic {
	return &AddProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddProductLogic) AddProduct(in *productrpc.AddProductRequest) (*productrpc.AddProductResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	product = mysql.Product{
		UserId:       in.UserId,
		Title:        in.Title,
		Price:        float64(in.Price),
		BrowseNumber: 0,
		Image:        in.Image,
		ClassId:      in.ClassId,
	}
	err := mysql.DB.Debug().Create(&product).Error
	if err != nil {
		return nil, fmt.Errorf("商品添加失败")
	}

	return &productrpc.AddProductResponse{Id: int64(product.ID)}, nil
}
